<?php


//namespace Celebrio\Util;

use \Nette\Object;
use Celebrio\WindowsAzure\AzureStorage\AzureStorageTable;
use \Microsoft_WindowsAzure_Storage_DynamicTableEntity;
use \Microsoft_WindowsAzure_Storage_TableEntity;
use \Microsoft_Http_Client_Adapter_Exception;
use \Microsoft_WindowsAzure_Exception;



class AzureTables extends Object
{

    private $tableName = null;
    private $table = null;

    public function __construct($tableName) {
        $this->tableName = $tableName;
    }

    public function save($partitionKey, $rowKey, $data) {


        try {
            $this->getTable()->createTableIfNotExists($this->tableName);
        } catch(Microsoft_WindowsAzure_Exception $e) {
            return false;
        }

        $entity = new Microsoft_WindowsAzure_Storage_DynamicTableEntity($partitionKey, $rowKey);

        foreach ($data as $key => $value) {
            $entity->setAzureProperty($key, $value);
        }

        $exist = $this->loadEntity($partitionKey, $rowKey);
        if(!$exist) {
            $this->getTable()->insertEntity($this->tableName, $entity);
        } else {
            $this->getTable()->updateEntity($this->tableName, $entity);
        }


    }

    public function test() {
        $entities = $this->getTable()->retrieveEntities($this->tableName);
        foreach ($entities as $entity) {

//            \Nette\debug::dump($entity->getAzureValues());
            $this->deleteEntity($entity);
        }

        throw new \Exception();
    }

    public function loadAll() {
        try {
            $entities = $this->getTable()->retrieveEntities($this->tableName);
        } catch(Microsoft_WindowsAzure_Exception $e) {
            return false;
        }

        $parsedEntities = array();

        foreach ($entities as $entity) {
            $parsedEntities[] = $this->parseEntity($entity);
        }
        return $parsedEntities;
    }

    public function load($partitionKey, $rowKey) {
        $entity = $this->loadEntity($partitionKey, $rowKey);
        if(!$entity) {
            return false;
        }

        return $this->parseEntity($entity);

    }
/*
    public function loadAll() {
        try {
            $entities = $this->getTable()->retrieveEntities($this->tableName);
        } catch(Microsoft_WindowsAzure_Exception $e) {
            return false;
        }

        $arrayValues = array();
        foreach ($entities as $entity) {
            $arrayValues[] = $this->parseEntity($entity);
        }

        return $arrayValues;
    }*/

    private function parseEntity($entity) {
        $values = array();
        foreach($entity->getAzureValues() as $property) {
            switch($property->Name) {
                case "PartitionKey":
                case "RowKey":
                case "Timestamp":
                    break;
                default:
                    $values[$property->Name] = $property->Value;
            }
        }

        return $values;
    }

    public function loadEntity($partitionKey, $rowKey) {
        try {
            $entity = $this->getTable()->retrieveEntityById($this->tableName, $partitionKey, $rowKey);
            return $entity;
        } catch(Microsoft_WindowsAzure_Exception $e) {
            return false;
        }
    }

    public function delete($partitionKey, $rowKey) {
        $entity = $this->loadEntity($partitionKey, $rowKey);

        if(!$entity) {
            return false;
        }
        $this->deleteEntity($entity);

    }

    public function deleteEntity($entity) {
        try {
            $this->getTable()->deleteEntity($this->tableName, $entity);
        } catch(Microsoft_WindowsAzure_Exception $e) {
            return false;
        }
    }


    private function getTable() {
        if(!isset($this->table)) {
            $this->table = AzureStorageTable::connection();
        }
        return $this->table;

    }
}
